export default {
	data() {
		return {
			list: [],
			options: {
				has_more: true,
				page: 1
			}
		};
	},
	methods: {
		renderPage(result, type = 'append') {
			if (!result || !result.data) {
				return
			}
			if (result.data.data.length == 0) {
			  this.options.has_more = false;
				if (type != 'append') {
					this.list = []
				}
			} else {
			  this.options.page++;
			  if (result.data.meta.current_page == 1) {
					this.list = result.data.data;
			  } else {
					this.list = [...this.list, ...result.data.data];
			  }
			}
		},
		currentPage: () => {
			let pages = getCurrentPages()
			let page = pages[pages.length - 1].route
			return page
		},
		async freshPage() {
			this.options.page = 1;
			this.options.has_more = true;
			let result = await this.fetchPageData('onPullDownRefresh');
			await this.renderPage(result, 'replace');
		},
	},
	async onPullDownRefresh () {
		let page = this.currentPage()
		console.log(`${page} onPullDownRefresh...`);
		await this.freshPage()
		uni.stopPullDownRefresh()
	},
	async onReachBottom() {
		let page = this.currentPage()
		console.log(`${page} onReachBottom...`);
		if (this.options.has_more) {
		  let result = await this.fetchPageData('onReachBottom');
		  this.renderPage(result);
		}
	}
	
}